package com.learn.mvc.handler;

import com.learn.mvc.entity.Soldier;
import com.learn.mvc.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

// @Controller
@RestController
public class AjaxHandler {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    // 使用 @ResponseBody注解告诉 SpringMVC：请你拿当前方法的返回值作为响应体，而不是逻辑视图
    // 方法返回值两种情况：
    // 情况一：简单类型。SpringMVC 会直接作为响应体数据
    // 情况二：复杂类型。SpringMVC 会把它转换为 JSON 然后作为响应体，此时需要 Jackson 的支持
    // @ResponseBody
    @RequestMapping("/ajax/experiment/one")
    public String experimentOne(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {

        logger.debug(username);
        logger.debug(password);

        // 服务器端给 Ajax 程序发送响应数据通过 handler 方法的返回值提供
        return "message from handler as response[来自服务器的问候]";
    }

    // @ResponseBody
    @RequestMapping("/ajax/experiment/two")
    public String experimentTwo(@RequestBody Student student) {
        logger.debug(student.toString());
        return "message from handler as response[来自服务器的问候]";
    }

    // @ResponseBody
    @RequestMapping("/ajax/experiment/three")
    public String experimentThree(
            // 请求参数名正好对应这个实体类的属性名，可以通过 setXxx() 方法直接注入
            Soldier solider
    ) {
        logger.debug(solider.toString());
        return "message from handler as response[来自服务器的问候]";
    }


    // @ResponseBody
    @RequestMapping("/ajax/experiment/four")
    public Soldier getSoldier() {
        return new Soldier(1,"spire man");
    }


}
